package com.jcnet.bizaia;
import org.apache.http.Header;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcnet.bizaia.bean.SPUserBean;
import com.jcnet.bizaia.bean.StateBean;
import com.jcnet.bizaia.tools.Toolkit;
import com.jcnet.bizaia.view.ProgressHUD;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
public class UserEditNikeName extends JCSimpleSherlockActivity {

	
	private EditText editText ;
	private String nikeName;
	
	private ProgressHUD progressHUD;
	
	
	private Context context;
	
	private SharedPreferences sharedPreferences;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
	
		setContentView(R.layout.user_edit_nike);
		sharedPreferences = getSharedPreferences(SPUserBean.dataTableNameForUser, MODE_PRIVATE);
		
		super.jcTitle=getString(R.string.userInfo);
		initActionBar();
		context = this;
		editText = (EditText)findViewById(R.id.userNikeName);
		
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		 menu.add(getText(R.string.success))
         //.setIcon(R.drawable.menus_bgg)
         .setTitle(getText(R.string.success))
         .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		return super.onCreateOptionsMenu(menu);
	}
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		
		nikeName = editText.getText().toString().trim();
		
		if(item.getTitle().equals(getText(R.string.success))){
			
			if(!TextUtils.isEmpty(nikeName)){
				if(Toolkit.isAvaliable(UserEditNikeName.this)){
					RequestParams params = new RequestParams();
					params.put("nickname", nikeName);
					params.put("imgidNum", 1+"");
					params.put("imgurl", "");  //
					params.put("useridNum", sharedPreferences.getInt(SPUserBean.userIDSP_1, -400));  //用户ID
					
					new AsyncHttpClient().post(App.HTTP_PATH+"use_updateUserOne.action?", params, new AsyncHttpResponseHandler(){
						
						@Override
						public void onStart() {
							// TODO Auto-generated method stub
							progressHUD = ProgressHUD.show(context, getString(R.string.commiting), true	, false, null);
							super.onStart();
						}
						
						
						@Override
						public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
							// TODO Auto-generated method stub
							super.onSuccess(arg0, arg1, arg2);
							
							StateBean stateBean = new Gson().fromJson(new String(arg2), new TypeToken<StateBean>(){}.getType());
							
							if(stateBean.getState()==200){
								sharedPreferences
								.edit()
								.putString(SPUserBean.nikeNameSP_2, nikeName)
								.commit();
								
								finish();
								
							}else{
								Toast.makeText(context, stateBean.getMessage()+"", 0).show();
							}
						}
						
						
						@Override
						public void onFinish() {
							// TODO Auto-generated method stub
							super.onFinish();
							if(progressHUD.isShowing()){
								progressHUD.cancel();
							}
						}
						
						@Override
						public void onFailure(int arg0, Header[] arg1, byte[] arg2,
								Throwable arg3) {
							// TODO Auto-generated method stub
							super.onFailure(arg0, arg1, arg2, arg3);
							if(progressHUD.isShowing()){
								progressHUD.cancel();
							}
							Toast.makeText(context, getText(R.string.serverError), 0).show();
						}
					});
					
					
				}else{
					Toast.makeText(UserEditNikeName.this, R.string.noNetWork, 0).show();
				}
			}else{
				Toast.makeText(UserEditNikeName.this, getString(R.string.contentNotEmpty), 0).show();
			}
			
			
			
			
			
			
		}
		
		
		
		return super.onOptionsItemSelected(item);
		
		
	}
}
